// ---------------------------------------------------------------------
//
// -- 游戏通用类包
// -- author £
//
// ---------------------------------------------------------------------

var common = {
    //定时器控制
    MyScheduler: cc.Class({
        name: "MyScheduler",
        properties: {
            _dic: null,
            _node: null,
            _startStamp: 0,
            _interval: 0,
        },

        deltaTime: function (key) {
            // let tmp = Math.round((new Date().getTime() - this._dic[key].startStamp) / 
            //     1000 / this._dic[key].interval);
            let tmp = Math.round((new Date().getTime() - this._dic[key].startStamp) /
                1000);

            return tmp >= 0 ? tmp : 0;
        },

        ctor: function () {
            this._dic = {};
            this._node = new cc.Node();
        },

        schedule: function (callBack, interval, repeat, delay, field, key) {
            this.unschedule(key);
            this._dic[key] = {};
            this._dic[key].startStamp = new Date().getTime();
            this._dic[key].interval = interval;

            if (repeat === -1 || repeat === cc.macro.REPEAT_FOREVER) {
                this._dic[key].action = cc.sequence(
                    cc.delayTime(delay),
                    cc.callFunc(
                        function () {
                            this._dic[key].action = cc.repeatForever(
                                cc.sequence(
                                    cc.delayTime(interval),
                                    cc.callFunc(callBack, field)
                                )
                            );
                            this._node.runAction(this._dic[key].action);
                        }, this
                    )
                );
            } else {
                this._dic[key].action = cc.sequence(
                    cc.delayTime(delay),
                    cc.repeat(
                        cc.sequence(
                            cc.delayTime(interval),
                            cc.callFunc(callBack, field)
                        )
                        , repeat)
                );
            }

            this._node.runAction(this._dic[key].action);
        },

        scheduleOnce: function (callBack, delay, field, key) {
            this.unschedule(key);
            this._dic[key] = {};
            this._dic[key].action = cc.sequence(
                cc.delayTime(delay),
                cc.callFunc(callBack, field)
            );
            this._node.runAction(this._dic[key].action);
        },

        unschedule: function (key) {
            if (this._dic[key] === undefined) return;

            this._node.stopAction(this._dic[key].action);
            delete this._dic[key];
        },

        unscheduleAllCallbacks: function () {
            this._dic = {};
            this._node.stopAllActions();
        },
    }),

    //radioGroup单选控制器
    RadioGroup: cc.Class({
        name: "RadioGroup",
        properties: {
            selectedIndex: {
                get: function () {
                    return this._selectedIndex;
                },
                set: function (value) {
                    this._selectedIndex = value;
                    this.setSelected(value);
                }
            },
            selectedItem: {
                set: function (value) {
                    let index = this._radios.indexOf(value);

                    this.selectedIndex = index;
                }
            }

        },

        ctor: function () {
            this._radios = null;
            this._rLen = 0;
            this._selectedIndex = -1;
            this._node;
        },

        update: function (arr, node) {
            let i, len;

            this._node = node;
            this._radios = arr;
            this._rLen = this._radios.length;
            this._selectedIndex = -1;

            for (i = 0; i < this._rLen; ++i) {
                this._radios[i].node.off('click', this.onSelected, this);
                this._radios[i].node.on('click', this.onSelected, this);
            }
        },

        onSelected: function (event) {
            let index = this._radios.indexOf(event.target.getComponent(cc.Toggle));

            this._selectedIndex = index;
            this.setSelected(index, true);
        },

        setSelected: function (index, mannul) {
            for (let i = 0; i < this._rLen; ++i) {
                if (i !== index) {
                    this._radios[i].isChecked = false;
                }
                else {
                    if (!this._radios[i].isChecked && !mannul)
                        this._radios[i].isChecked = true;
                }

                this._radios[i].interactable = (i !== index);
            }

            if (this._node) {
                this._node.emit('change', { index: this._selectedIndex, mannul: mannul });
            }
        }
    }),

    //锁定管理
    LockManager: cc.Class({
        name: "LockManager",
        properties: {

        },

        ctor: function () {
            this._dic = {};
            // delete this._dic[key];
        },

        start: function (name) {
            if (!this._dic[name])
                this._dic[name] = true;
        },

        end: function (name) {
            if (this._dic[name])
                delete this._dic[name];
        },

        isFree: function () {
            let count = 0;

            for (let key in this._dic) {
                ++count;
            }

            return count === 0;
        },

        clear: function () {
            this._dic = {};
        },
    }),

    //分页列表管理
    PageListManager: cc.Class({
        name: "PageListManager",
        properties: {

        },

        ctor: function () {
            this._list = null;
            this._pItr = -1;
            this._totalPage = -1;
            // delete this._dic[key];
        },

        init: function (row) {
            this._row = row;
        },

        setData: function (list) {
            this._list = list;
            this._pItr = 0;
            this._totalPage = Math.ceil(list.length / this._row);
            return this._getPageData(0);
        },

        pre: function () {
            if (this._pItr - 1 < 0) return null;

            return this._getPageData(--this._pItr);
        },

        next: function () {
            if (this._pItr + 1 >= this._totalPage) return null;

            return this._getPageData(++this._pItr);
        },
        
        _getPageData: function (page) {
            let result = [],
                start = this._row * page;
            
            for (let i = 0; i < this._row; ++i) {
                if ((start + i) === this._list.length) break;
    
                result[i] = this._list[start + i];
            }
    
            return result;
        },

        first: function () {
            return this._getPageData(0);
        },
    }),
};

module.exports = common;